const path = require('path');
const webpack = require('webpack');
const { devServer, meta } = require('./.medAdminrc.js');

function resolve(dir) {
  return path.join(__dirname, './', dir);
}

module.exports = {
  devServer: {
    overlay: {
      warnings: false,
      errors: true,
    },
    disableHostCheck: true,
    ...devServer,
  },
  chainWebpack: config => {
    config.resolve.alias
      .set('vue$', 'vue/dist/vue.esm.js')
      .set('@', resolve('src'))
      .set('~', path.join(__dirname, './'));

    config.plugin('html').tap(args => {
      args[0].title = meta.defaultTitle;
      return args;
    });

    config
      .plugin('ignore')
      .use(new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /(zh-cn)$/));

    config.optimization.splitChunks({
      chunks: 'all',
      cacheGroups: {
        element: {
          name: 'vendor-element',
          priority: 15,
          test: /[\\/]node_modules[\\/]element-ui[\\/]/,
        },
        // 第三方公共包
        vendor: {
          name: 'vendor',
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: 'initial', // 只打包初始时的依赖
        },
        common: {
          name: 'common',
          minSize: 30,
          minChunks: 2, //模块被引用2次以上的才抽离
          priority: 0,
          reuseExistingChunk: true,
        },
      },
    });
  },
};
